//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.planw.beetl.utils;

import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.PopupChooserBuilder;
import com.intellij.ui.components.JBList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class UiUtils {
  public UiUtils() {
  }

  public static JBPopup showListPopup(@NotNull String title, Project project, @Nullable ListSelectionListener listener, @NotNull Object[] objs) {
    if (title == null) {
      return null;
    }

    if (objs == null) {
      return null;
    }

    PopupChooserBuilder builder = createListPopupBuilder(title, listener, objs);
    JBPopup popup = builder.createPopup();
    setPositionForShown(popup, project);
    return popup;
  }

  protected static PopupChooserBuilder createListPopupBuilder(@NotNull String title, final @Nullable ListSelectionListener listener, Object... objs) {
    if (title == null) {
      return null;
    }

    if (objs == null) {
      return null;
    }

    final JBList list = new JBList(objs);
    PopupChooserBuilder builder = new PopupChooserBuilder(list);
    builder.setTitle(title);
    if (null != listener) {
      Runnable var10000 = new Runnable() {
        @Override
        public void run() {
          listener.selected(list.getSelectedIndex());
        }
      };
      builder.setItemChoosenCallback(() -> listener.selected(list.getSelectedIndex()));
    }

    return builder;
  }

  private static void setPositionForShown(JBPopup popup, Project project) {
    Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
    if (null != editor) {
      popup.showInBestPositionFor(editor);
    } else {
      popup.showCenteredInCurrentWindow(project);
    }

  }

  public interface ListSelectionListener {
    void selected(int var1);
  }
}
